package com.woniu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.entity.Cart;
import com.woniu.entity.Order;
import com.woniu.entity.Orderitem;
import com.woniu.entity.Product;
import com.woniu.mapper.CartMapper;
import com.woniu.mapper.OrderMapper;
import com.woniu.mapper.OrderitemMapper;
import com.woniu.mapper.ProductMapper;
import com.woniu.service.CartService;
import com.woniu.service.OrderService;
import com.woniu.vo.CartItemVo;
import com.woniu.vo.OrderPreviewVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.function.Consumer;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author huangjianbin
 * @since 2023-02-07 11:59:17
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Autowired
    CartMapper cartMapper;

    @Autowired
    ProductMapper productMapper;

    @Autowired
    CartService cartService;

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    OrderitemMapper orderitemMapper;

    @Override
    public OrderPreviewVo queryOrderPreview(Integer id, Integer[] productIds) {
        OrderPreviewVo orderPreviewVo = new OrderPreviewVo();

        List<CartItemVo> cartItemVoList = new ArrayList<>();

        //查询出所有被选择的购物项
        QueryWrapper<Cart> cartQueryWrapper = new QueryWrapper<>();
        cartQueryWrapper.eq("user_id",id);
        cartQueryWrapper.and(new Consumer<QueryWrapper<Cart>>() {
            @Override
            public void accept(QueryWrapper<Cart> cartQueryWrapper) {
                cartQueryWrapper.in("product_id",productIds);
            }
        });

        List<Cart> carts = cartMapper.selectList(cartQueryWrapper);

        //所有Cart转换为CartItemVo   同时计算总价
        BigDecimal totalPrice = new BigDecimal("0.00");
        for(Cart cart : carts){
            CartItemVo cartItemVo = new CartItemVo();
            cartItemVo.setCartItemId(cart.getId());
            cartItemVo.setProductId(cart.getProductId());

            Product product = productMapper.selectById(cart.getProductId());
            cartItemVo.setProductName(product.getProductname());
            cartItemVo.setProductPrice(product.getProductprice());
            cartItemVo.setProductImgsrc(product.getProductimgsrc());

            cartItemVo.setBuycount(cart.getBuycount());
            cartItemVo.setSumprice(cart.getSumprice());

            totalPrice = totalPrice.add(cart.getSumprice());  //累计计算总价

            cartItemVoList.add(cartItemVo);
        }

        orderPreviewVo.setCartItemVoList(cartItemVoList);
        orderPreviewVo.setTotalPrice(totalPrice);

        return orderPreviewVo;
    }

    @Override
    public void createOrder(Integer userId, Integer[] productIds, Integer addressId) {
        //复用原有方法,查询订单中的商品信息和总价
        OrderPreviewVo orderPreviewVo = queryOrderPreview(userId, productIds);

        //生成主订单
        Order order = new Order();
        order.setOrdernum("WONIUZHOUKAO"+new Date().getTime());
        order.setUserId(userId);
        order.setAddressId(addressId);
        order.setStatus(1); //状态(1未支付,2已支付,3已取消)
        order.setTotalprice(orderPreviewVo.getTotalPrice());

        orderMapper.insert(order);  //写入完成后,id会自动赋值给order对象
        System.out.println("新订单的id: "+order.getId());

        //写入订单详情
        List<CartItemVo> cartItemVoList = orderPreviewVo.getCartItemVoList();
        for(CartItemVo cartItemVo : cartItemVoList){
            Orderitem orderitem = new Orderitem();
            orderitem.setProductId(cartItemVo.getProductId());
            orderitem.setProductname(cartItemVo.getProductName());
            orderitem.setProductprice(cartItemVo.getProductPrice());
            orderitem.setProductimgsrc(cartItemVo.getProductImgsrc());
            orderitem.setBuycount(cartItemVo.getBuycount());
            orderitem.setSumprice(cartItemVo.getSumprice());
            orderitem.setOrderId(order.getId());

            orderitemMapper.insert(orderitem);

            //删除购物车中的对应信息
            cartService.updatebuycount(userId,cartItemVo.getProductId(),0);
        }
    }
}
